In [1]:
import os
import numpy as np
import pandas as pd 
import imageio

import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image, ImageOps
import scipy.ndimage as ndi

from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPool2D, Dropout, Activation, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import ReduceLROnPlateau
from keras.preprocessing import image
from keras.utils import plot_model
Using TensorFlow backend.
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
WARNING:root:Limited tf.compat.v2.summary API due to missing TensorBoard installation.
In [2]:
dirname = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia'
train_nrml_pth = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/train/NORMAL/'
train_pnm_pth = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/train/PNEUMONIA/'
test_nrml_pth = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/test/NORMAL/'
test_pnm_pth ='C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/test/PNEUMONIA/'
val_nrml_pth ='C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/val/NORMAL/'
val_pnm_pth = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/val/PNEUMONIA/'
In [3]:
def plot_imgs(item_dir, num_imgs=25):
    all_item_dirs = os.listdir(item_dir)
    item_files = [os.path.join(item_dir, file) for file in all_item_dirs][:num_imgs]

    plt.figure(figsize=(10, 10))
    for idx, img_path in enumerate(item_files):
        plt.subplot(5, 5, idx+1)

        img = plt.imread(img_path)
        plt.imshow(img)

    plt.tight_layout()
In [4]:
    plot_imgs(train_nrml_pth)
In [5]:
    plot_imgs(train_pnm_pth)
In [6]:
def plot_img_hist(item_dir, num_img=6):
  all_item_dirs = os.listdir(item_dir)
  item_files = [os.path.join(item_dir, file) for file in all_item_dirs][:num_img]
  
  #plt.figure(figsize=(10, 10))
  for idx, img_path in enumerate(item_files):
    fig1 = plt.figure(idx,figsize=(10, 10))
    fig1.add_subplot(2, 2, 1)
    img = mpimg.imread(img_path, )
    plt.imshow(img)
    fig1.add_subplot(2, 2, 2)
    plt.hist(img.ravel(),bins=256, fc='k', ec='k')
  
  plt.tight_layout()
In [7]:
plot_img_hist(train_pnm_pth,2)
In [8]:
def plot_img_hist_ndi(item_dir, num_img=6):
  all_item_dirs = os.listdir(item_dir)
  item_files = [os.path.join(item_dir, file) for file in all_item_dirs][:num_img]
  
  #plt.figure(figsize=(10, 10))
  for idx, img_path in enumerate(item_files):
    im = imageio.imread(img_path)
    hist = ndi.histogram(im, min=0, max=255, bins=256)
    cdf = hist.cumsum() / hist.sum()
    
    fig1 = plt.figure(idx,figsize=(10, 10))
    fig1.add_subplot(2, 3, 1)
    img = mpimg.imread(img_path, )
    plt.title("No. {}".format(idx))
    plt.imshow(img)
    fig1.add_subplot(2, 3, 2)
    plt.title("Histogram")
    plt.plot(hist)
    fig1.add_subplot(2, 3, 3)
    plt.title("CDF")
    plt.plot(cdf)

  plt.tight_layout()
plot_img_hist_ndi(train_pnm_pth,2)
In [9]:
train_nrml_pth_work = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/train/NORMAL/'
train_pnm_pth_work = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/train/PNEUMONIA/'
test_nrml_pth_work ='C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/test/NORMAL/'
test_pnm_pth_work = 'C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/test/PNEUMONIA/'
In [10]:
def image_resizing(path_from, path_to, height=500, width=500):
    size = height, width
    i=1
    files = os.listdir(path_from)
    for file in files: 
        try:
            file_dir = os.path.join(path_from, file)
            file_dir_save = os.path.join(path_to, file)
            img = Image.open(file_dir)
            img = img.resize(size, Image.ANTIALIAS)
            img = img.convert("RGB")
            img.save(file_dir_save) 
            i=i+1
        except:
            continue
In [11]:
image_resizing(train_nrml_pth, train_nrml_pth_work, 300, 300)
image_resizing(train_pnm_pth, train_pnm_pth_work, 300, 300)
image_resizing(test_nrml_pth, test_nrml_pth_work, 300, 300)
image_resizing(test_pnm_pth, test_pnm_pth_work, 300, 300)
In [12]:
plot_imgs(train_nrml_pth_work)
In [13]:
plot_imgs(train_pnm_pth_work)
In [14]:
def  hist_equal(path_from, path_to):
    i=1
    files = os.listdir(path_from)
    for file in files: 
        try:
            file_dir = os.path.join(path_from, file)
            file_dir_save = os.path.join(path_to, file)
            img = Image.open(file_dir)
            img = ImageOps.equalize(img)
            #img = img.convert("RGB") #konwersja z RGBA do RGB, usuniecie kanału alfa zeby zapisać do jpg
            img.save(file_dir_save) 
            i=i+1
        except:
            continue
In [15]:
hist_equal(train_pnm_pth_work, train_pnm_pth_work)
hist_equal(train_nrml_pth_work, train_nrml_pth_work)

hist_equal(test_pnm_pth_work, test_pnm_pth_work)
hist_equal(test_nrml_pth_work, test_nrml_pth_work)
plot_img_hist(train_pnm_pth_work, 2)
In [16]:
plot_img_hist_ndi(train_pnm_pth_work, 2)
In [17]:
img_size_h = 300
img_size_w = 300

input_shape = (img_size_h, img_size_w, 1) 
model = Sequential([
    Conv2D(32, (3,3), input_shape=input_shape),
    MaxPool2D((2, 2)),
    
    Conv2D(32, (3,3)),
    MaxPool2D((2, 2)),
    
    Conv2D(64, (3,3)),
    MaxPool2D((2, 2)),
    
    Conv2D(64, (3,3)),
    MaxPool2D((2, 2)),
    
    Flatten(),
    
    Dense(128, activation='relu'),
    Dropout(0.5),
    Dense(1, activation='sigmoid')
    
    
])
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
WARNING:tensorflow:From c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\keras\backend\tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\keras\backend\tensorflow_backend.py:4070: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.

WARNING:tensorflow:From c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\ops\nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
WARNING:tensorflow:From c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\tensorflow\python\ops\nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
Model: "sequential_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 298, 298, 32)      320       
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 149, 149, 32)      0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 147, 147, 32)      9248      
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 73, 73, 32)        0         
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 71, 71, 64)        18496     
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 35, 35, 64)        0         
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 33, 33, 64)        36928     
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 16, 16, 64)        0         
_________________________________________________________________
flatten_1 (Flatten)          (None, 16384)             0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               2097280   
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 129       
=================================================================
Total params: 2,162,401
Trainable params: 2,162,401
Non-trainable params: 0
_________________________________________________________________
In [18]:
train_path_work='C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/train/'
test_path_work='C:/Users/ANUBHAV/Documents/chest-xray-pneumonia/test/'
In [19]:
train_datagen = ImageDataGenerator(
    rescale=1./255,    
    featurewise_center=True,
    featurewise_std_normalization=True,
    rotation_range=45,
    width_shift_range=0.5,
    height_shift_range=0.5,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

val_datagen = ImageDataGenerator(
    rescale=1./255,
    rotation_range=45,
    width_shift_range=0.5,
    height_shift_range=0.5,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)
In [20]:
batch_size = 32
train_generator = train_datagen.flow_from_directory(
    train_path_work,
    target_size=(img_size_h, img_size_w),
    color_mode='grayscale', #we use grayscale images I think
    batch_size=batch_size,
    class_mode='binary',
    shuffle=True, #we shuffle our images for better performance
    seed=8)

validation_generator = val_datagen.flow_from_directory(
    test_path_work,
    target_size=(img_size_h, img_size_w),
    color_mode='grayscale',
    batch_size=batch_size,
    class_mode='binary',
    shuffle=True,
    seed=8)
Found 5216 images belonging to 2 classes.
Found 624 images belonging to 2 classes.
In [21]:
training_examples = 5216
validation_examples = 624
In [22]:
learning_rate_reduction = ReduceLROnPlateau(monitor='val_accuracy', 
                                            patience=2, 
                                            verbose=1, 
                                            factor=0.5, 
                                            min_lr=0.0001) #0.00001
callback = [learning_rate_reduction]
In [23]:
history = model.fit_generator(
    train_generator,
    epochs=20,
    validation_data=validation_generator,
    callbacks = callback
    )
WARNING:tensorflow:From c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\keras\backend\tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.

WARNING:tensorflow:From c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\keras\backend\tensorflow_backend.py:422: The name tf.global_variables is deprecated. Please use tf.compat.v1.global_variables instead.

Epoch 1/20
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\keras_preprocessing\image\image_data_generator.py:716: UserWarning: This ImageDataGenerator specifies `featurewise_center`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.
  warnings.warn('This ImageDataGenerator specifies '
c:\users\anubhav\pycharmprojects\untitled3\venv\lib\site-packages\keras_preprocessing\image\image_data_generator.py:724: UserWarning: This ImageDataGenerator specifies `featurewise_std_normalization`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.
  warnings.warn('This ImageDataGenerator specifies '
163/163 [==============================] - 34s 209ms/step - loss: 0.6400 - accuracy: 0.7303 - val_loss: 0.6702 - val_accuracy: 0.6250
Epoch 2/20
163/163 [==============================] - 36s 222ms/step - loss: 0.5992 - accuracy: 0.7414 - val_loss: 0.7117 - val_accuracy: 0.6250
Epoch 3/20
163/163 [==============================] - 33s 204ms/step - loss: 0.6078 - accuracy: 0.7416 - val_loss: 0.6686 - val_accuracy: 0.6250

Epoch 00003: ReduceLROnPlateau reducing learning rate to 0.0005000000237487257.
Epoch 4/20
163/163 [==============================] - 34s 209ms/step - loss: 0.5957 - accuracy: 0.7429 - val_loss: 0.5778 - val_accuracy: 0.6250
Epoch 5/20
163/163 [==============================] - 35s 216ms/step - loss: 0.5857 - accuracy: 0.7429 - val_loss: 0.7480 - val_accuracy: 0.6250

Epoch 00005: ReduceLROnPlateau reducing learning rate to 0.0002500000118743628.
Epoch 6/20
163/163 [==============================] - 35s 215ms/step - loss: 0.5871 - accuracy: 0.7429 - val_loss: 0.6112 - val_accuracy: 0.6250
Epoch 7/20
163/163 [==============================] - 36s 219ms/step - loss: 0.5801 - accuracy: 0.7429 - val_loss: 0.7938 - val_accuracy: 0.6250

Epoch 00007: ReduceLROnPlateau reducing learning rate to 0.0001250000059371814.
Epoch 8/20
163/163 [==============================] - 35s 217ms/step - loss: 0.5824 - accuracy: 0.7429 - val_loss: 0.7804 - val_accuracy: 0.6250
Epoch 9/20
163/163 [==============================] - 34s 211ms/step - loss: 0.5790 - accuracy: 0.7429 - val_loss: 0.4729 - val_accuracy: 0.6250

Epoch 00009: ReduceLROnPlateau reducing learning rate to 0.0001.
Epoch 10/20
163/163 [==============================] - 37s 224ms/step - loss: 0.5854 - accuracy: 0.7429 - val_loss: 0.6838 - val_accuracy: 0.6250
Epoch 11/20
163/163 [==============================] - 36s 221ms/step - loss: 0.5778 - accuracy: 0.7429 - val_loss: 0.7311 - val_accuracy: 0.6250
Epoch 12/20
163/163 [==============================] - 35s 217ms/step - loss: 0.5781 - accuracy: 0.7429 - val_loss: 0.6391 - val_accuracy: 0.6250
Epoch 13/20
163/163 [==============================] - 36s 220ms/step - loss: 0.5797 - accuracy: 0.7429 - val_loss: 0.5653 - val_accuracy: 0.6250
Epoch 14/20
163/163 [==============================] - 37s 227ms/step - loss: 0.5780 - accuracy: 0.7429 - val_loss: 0.6191 - val_accuracy: 0.6250
Epoch 15/20
163/163 [==============================] - 36s 221ms/step - loss: 0.5802 - accuracy: 0.7429 - val_loss: 0.7714 - val_accuracy: 0.6250
Epoch 16/20
163/163 [==============================] - 37s 227ms/step - loss: 0.5747 - accuracy: 0.7427 - val_loss: 0.6125 - val_accuracy: 0.6250
Epoch 17/20
163/163 [==============================] - 37s 225ms/step - loss: 0.5741 - accuracy: 0.7427 - val_loss: 0.7813 - val_accuracy: 0.6250
Epoch 18/20
163/163 [==============================] - 36s 223ms/step - loss: 0.5706 - accuracy: 0.7429 - val_loss: 0.6321 - val_accuracy: 0.6250
Epoch 19/20
163/163 [==============================] - 37s 224ms/step - loss: 0.5734 - accuracy: 0.7433 - val_loss: 0.5730 - val_accuracy: 0.6250
Epoch 20/20
163/163 [==============================] - 37s 227ms/step - loss: 0.5684 - accuracy: 0.7462 - val_loss: 0.6118 - val_accuracy: 0.6250
In [24]:
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')
plt.show()
In [25]:
img_test_path = os.path.join(test_nrml_pth, 'NORMAL2-IM-0337-0001.jpeg')
img_train_path_ill = os.path.join(train_pnm_pth, 'person1787_bacteria_4634.jpeg')
img_p = image.load_img(img_test_path, target_size=(img_size_h, img_size_w), color_mode='grayscale')
img_arr_p = np.array(img_p)
img_arr_p = np.expand_dims(img_arr_p, axis=0)
img_arr_p = np.expand_dims(img_arr_p, axis=3)
images_p = np.vstack([img_arr_p])
In [26]:
def predict_illness(image_path):
    imge = plt.imread(image_path)
    plt.imshow(imge)

    img = image.load_img(image_path, target_size=(img_size_h, img_size_w), color_mode='grayscale')
    x = image.img_to_array(img) 
    x = np.expand_dims(x, axis=0)
    images = np.vstack([x])

    classes = model.predict_classes(images, batch_size=10)
    if classes[0][0] == 0:
        print("They got healthy!")
    else:
        print("They got pneumonia!")
In [27]:
predict_illness(img_train_path_ill)
They got pneumonia!
In [28]:
from keras.models import Model
layer_outputs = [layer.output for layer in model.layers[:len(model.layers)]]
activation_model = Model(inputs=model.input, outputs=layer_outputs)
activations = activation_model.predict(images_p)

first_layer_activation = activations[0]
plt.matshow(first_layer_activation[0, :, :, 4], cmap='viridis')
Out[28]:
<matplotlib.image.AxesImage at 0x1312a1ecc18>
In [29]:
model.layers[:-1]# Droping The Last Dense Layer
Out[29]:
[<keras.layers.convolutional.Conv2D at 0x1312a561048>,
 <keras.layers.pooling.MaxPooling2D at 0x1312a544ac8>,
 <keras.layers.convolutional.Conv2D at 0x1312c03bb38>,
 <keras.layers.pooling.MaxPooling2D at 0x1312a54f908>,
 <keras.layers.convolutional.Conv2D at 0x1312a54f5f8>,
 <keras.layers.pooling.MaxPooling2D at 0x1312a415630>,
 <keras.layers.convolutional.Conv2D at 0x1312a415470>,
 <keras.layers.pooling.MaxPooling2D at 0x1312a415b00>,
 <keras.layers.core.Flatten at 0x1312a415cf8>,
 <keras.layers.core.Dense at 0x1312a415898>,
 <keras.layers.core.Dropout at 0x1312a415da0>]
In [30]:
layer_names = []
for layer in model.layers[:-1]:
    layer_names.append(layer.name) 
images_per_row = 16
zipped_layers = zip(layer_names, activations)
for layer_name, layer_activation in zipped_layers: #this loop     
    if layer_name.startswith('conv'):
        n_features = layer_activation.shape[-1]
        size = layer_activation.shape[1]
        n_cols = n_features // images_per_row
        display_grid = np.zeros((size * n_cols, images_per_row * size))
        for col in range(n_cols):
            for row in range(images_per_row):
                channel_image = layer_activation[0,:, :, col * images_per_row + row]
                channel_image -= channel_image.mean()
                channel_image /= channel_image.std()
                channel_image *= 64
                channel_image += 128
                channel_image = np.clip(channel_image, 0, 255).astype('uint8')
                display_grid[col * size : (col + 1) * size,
                             row * size : (row + 1) * size] = channel_image
        scale = 1. / size
        plt.figure(figsize=(scale * display_grid.shape[1],
                            scale * display_grid.shape[0]))
        plt.title(layer_name)
        plt.grid(False)
        plt.imshow(display_grid, aspect='auto', cmap='viridis')
In [31]:
layer_names = []
for layer in model.layers[:-1]:
    layer_names.append(layer.name) 
images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
    if layer_name.startswith('max'):
        n_features = layer_activation.shape[-1]
        size = layer_activation.shape[1]
        n_cols = n_features // images_per_row
        display_grid = np.zeros((size * n_cols, images_per_row * size))
        for col in range(n_cols):
            for row in range(images_per_row):
                channel_image = layer_activation[0,:, :, col * images_per_row + row]
                channel_image -= channel_image.mean()
                channel_image /= channel_image.std()
                channel_image *= 64
                channel_image += 128
                channel_image = np.clip(channel_image, 0, 255).astype('uint8')
                display_grid[col * size : (col + 1) * size,
                             row * size : (row + 1) * size] = channel_image
        scale = 1. / size
        plt.figure(figsize=(scale * display_grid.shape[1],
                            scale * display_grid.shape[0]))
        plt.title(layer_name)
        plt.grid(False)
        plt.imshow(display_grid, aspect='auto', cmap='viridis')
In [ ]: